/***********************************************************
 *  File_Name  : XJBase64.cpp
 *  File_Path   : 
 *  File_Encode : UTF8
 *  Description : BASE64编码,解码
 *  
 *  OS : Linux, UNIX,Windows,Other
 *  LastVersion  : 20111118
 *      
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2011-11-18
 *  Version     : 20111118
 *  Description : 
 *                
 *  Edit Author : 
 *  Edit Date   : 
 *  Version     : 
 *  Description :
 *                
 ***********************************************************/
#include "XJCharCode.h"

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <iconv.h>
namespace XJUtilClass{
XJCharCode::XJCharCode() {
	// TODO Auto-generated constructor stub

}

XJCharCode::~XJCharCode() {
	// TODO Auto-generated destructor stub
}
int XJCharCode::utf8togb2312(char *inbuf,int inlen,char *outbuf,int outlen){
	return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
int XJCharCode::gb2312toutf8(char *inbuf,int inlen,char *outbuf,int outlen){
	return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
int XJCharCode::utf8togbk(char *inbuf,int inlen,char *outbuf,int outlen){
	return code_convert("utf-8","gbk",inbuf,inlen,outbuf,outlen);
}
int XJCharCode::gbktoutf8(char *inbuf,int inlen,char *outbuf,int outlen){
	return code_convert("gbk","utf-8",inbuf,inlen,outbuf,outlen);
}
//代码转换:从一种编码转为另一种编码
int XJCharCode::code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
	iconv_t cd;
	int rc = 0;
	char **pin = &inbuf;
	char **pout = &outbuf;
	cd = iconv_open(to_charset, from_charset);
	if (cd==0) return -1;
	memset(outbuf,0,outlen);
	size_t nsInLen = inlen;
	size_t nsOutLen = outlen;
	if (iconv(cd,pin,(size_t*)&nsInLen,pout,(size_t*)&nsOutLen)==-1) rc = -1;
	iconv_close(cd);
	return rc;
}
}//namespace XJUtilClass
